<?php
namespace YunCangChu\Controller;
use Guzzle\Plugin\Cookie\Cookie;
use Think\Controller;
class StorehouseController extends PublicController {
    //有仓分享跳转 这里判断此是否已经加盟仓库
    public function index(){
        /*（N：待审核。跳转到显示加盟信息页面，可以修改；不能维护仓库信息
        E：审核中。跳转到显示加盟信息页面，不能修改；不能维护仓库信息
        Y：审核通过。跳转到仓库列表，可维护仓库信息
        C：审核不通过。跳转到显示加盟信息页面，不能修改；不能维护仓库信息
        U：未提交加盟信息。跳转到申请加盟页面）
        */
        Cookie('ji',null);
        Cookie('ji',2);
        //$_COOKIE['ji'] = 2;
        $url = C('api_url')['获取加盟信息'];
        //请求参数
        $data['FuncName'] = 'GET_INFO_JOININ';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        //$this->redirect('shenQing');
        //dump($return_arr);die;
        if($return_arr['ReturnCode'] == 0)
        {
            //Y审核通过。分享的仓
            if($return_arr['ReturnData']['Status'] == 'Y')
            {
                $this->redirect('join');
            }
            //未提交加盟信息。跳转到申请加盟页面
            elseif($return_arr['ReturnData']['Status'] == 'U')
            {
                $this->redirect('shenQing');
            }
            echo $return_arr['ReturnData']['Status'];die;
        }
        else
        {
            //输出错误信息
            echo $return_arr['ReturnText'];
        }
    }
    //已加盟 分享的仓
    public function join()
    {
        //$url = C('api_url')['获取加盟信息'];
        $this->display();
    }
    //加盟信息
    public function joinXinXi()
    {
        $url = C('api_url')['获取加盟信息'];
        //请求参数
        $data['FuncName'] = 'GET_INFO_JOININ';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->assign('data',$return_arr['ReturnData']);
        $this->display();
    }
    //申请加盟
    public function shenQing()
    {

        $key1 = '申请加盟身份证照片-'.$this->LoginID;
        $key2 = '申请加盟身份证背面照-'.$this->LoginID;
        $key3 = '申请加盟手持身份证照-'.$this->LoginID;
        S($key1,null);
        S($key2,null);
        S($key3,null);
        $this->display();
    }
    //我的仓库
    public function mycangku()
    {
        //我的仓列表
        $url = C('api_url')['我的仓列表'];
        $data['FuncName'] = 'GET_MYWH_LIST';
        $data['LoginID'] = $this->LoginID;
        $data['PageIndex'] = 1;
        if(I('get.FiltStr') == '')
        {
            $data['FiltStr'] = 'A';
        }
        else
        {
            $data['FiltStr'] = I('get.FiltStr');
        }
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr['ReturnData']['rows']);die;
        if($return_arr['ReturnCode'] == 0)
        {
            $this->assign('FiltStr',$data['FiltStr']);
            $this->assign('data',$return_arr['ReturnData']['rows']);
            $this->display();
        }
        else
        {
            //输出错误信息
            echo $return_arr['ReturnText'];
        }
    }
    //我的仓库列表分页
    public function fenye1()
    {
        $url = C('api_url')['我的仓列表'];
        $data['PageIndex'] = I('post.page');
        //$data['PageIndex'] = 1;
        $data['FiltStr'] =I('post.select');
        //$data['FiltStr'] = 'A';
        $data['FuncName'] = 'GET_MYWH_LIST';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        $data = $return_arr['ReturnData']['rows'];
        if(count($data) > 0)
        {
            $str = '';
            foreach($data as $key =>$val)
            {
                if($val['PhotoUrl'] == '')
                {
                    $str.='<li class="mui-table-view-cell mui-media">
					<a href="'.U('GET_MYWH',array('id'=>$val['StoreID'])).'" class="wspin">
													<img class="mui-media-object mui-pull-left mui-sc-img" src="http://bpic.588ku.com/element_origin_min_pic/01/47/02/12574338a640011.jpg">
				';
                }
                else
                {
                    $str.='<li class="mui-table-view-cell mui-media">
					<a href="'.U('GET_MYWH',array('id'=>$val['StoreID'])).'" class="wspin">
													<img class="mui-media-object mui-pull-left mui-sc-img" src="'.$val['PhotoUrl'].'">
				';  
                }    
                
				$str.='	<div class="mui-media-body mui-sc-list">
							<p class="mui-sc-p">'.$val['Name'].'</p>
							<p><lable>位置:</lable><i>'.$val['Addr'].'</i></p>
							<p><lable>价格:</lable><i>'.$val['PriceCust'].'元</i></p>';
                if($val['Examined'] == 'FALSE')
                {
                    $str.= '<button type="button" class="mui-btn mui-dsh-icon"><i></i> 未审核</button>
						</div>
					</a>
				</li>';
                }
                else
                {
                    $str.= '<button type="button" class="mui-btn mui-wcz-icon"><i></i> 已审核</button>
						</div>
					</a>
				</li>';
                }

            }
            $this->ajaxReturn(array('data'=>$str,'code'=>200));

        }
        else
        {
            $this->ajaxReturn(array('data'=>'','code'=>404));
        }
    }
    //我的仓库 百度地图版
    public function mycangkubaidu()
    {
        //获取我的全部仓库位置
        $url = C('api_url')['我的仓列表'];
        $data['FuncName'] = 'GET_MYWH_LIST';
        $data['LoginID'] = $this->LoginID;
        $data['PageIndex'] = 0;
        $data['FiltStr'] = 'A';
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr);die;
        $this->assign('data',$return_arr['ReturnData']['rows']);
        $this->display();
    }
    //我的仓库 仓库详情
    public function GET_MYWH()
    {

        $url = C('api_url')['我的仓库详情'];
        $data['StoreID'] = I('get.id');
        $data['LoginID'] = $this->LoginID;
        $data['FuncName'] = 'GET_MYWH';
        $app_date = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_date));
        //dump($return_arr);die;
        if($return_arr['ReturnCode'] == 0)
        {
            $this->assign('data',$return_arr['ReturnData']);
            $this->display();
        }
        else
        {
            echo $return_arr['ReturnText'];
        }
    }
    //添加仓库
    //C:\Users\hkzr\Desktop\百福云仓-标注切图\百福云仓-标注切图\安卓-切图标注\计时租赁切图\计时租赁切图\计时租赁切图\drawable-hdpi
    public function addCangku()
    {
        header('content-type:text/html;charset=utf8');
        S('仓库平面图-'.$this->LoginID,null);
        S('仓库房产证-'.$this->LoginID,null);
        S('仓库照片-'.$this->LoginID,null);
        $url = C('api_url')['仓储类别'];
        $data['FuncName'] = 'GET_CATEGORY_WH';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        //dump($return_arr['ReturnData']['rows']);die;
        $this->assign('type',$return_arr['ReturnData']['rows']);
        //获取省市县
        $url = C('api_url')['全国行政三级联动数据'];
        $data=array();
        $data['FuncName'] = 'GET_REGION';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        S('city',$return_arr,86400);
        $this->assign('data',$return_arr);
        //获取省市县
        $url = C('api_url')['仓库的建筑结构'];
        $data=array();
        $data['FuncName'] = 'GET_BUILDING_STRU';
        $data['LoginID'] = $this->LoginID;
        $app_data = baseMD5($data);
        //返回的参数
        $return_arr = (cUrl($url,$header=null, $app_data));
        $this->assign('type1',$return_arr['ReturnData']['rows']);
        $this->display();
    }
    //获取更多城市
    public function get_city()
    {
        $id = I('post.id');
        if(S('city') == '')
        {
            //获取省市县
            $url = C('api_url')['全国行政三级联动数据'];
            $data=array();
            $data['FuncName'] = 'GET_REGION';
            $data['LoginID'] = $this->LoginID;
            $app_data = baseMD5($data);
            //返回的参数
            $return_arr = (cUrl($url,$header=null, $app_data));
            S('city',$return_arr,86400);
        }
        foreach(S('city')['ReturnData'] as $key =>$val)
        {
            if($val['ProvID'] == $id)
            {
                $this->ajaxReturn($val['Citys']);
                break;
            }
        }
    }
    //获取更多城市
    public function get_city1()
    {
        //省
        $cid = I('post.cid');
        //市
        $id = I('post.id');
        if(S('city') == '')
        {
            //获取省市县
            $url = C('api_url')['全国行政三级联动数据'];
            $data=array();
            $data['FuncName'] = 'GET_REGION';
            $data['LoginID'] = $this->LoginID;
            $app_data = baseMD5($data);
            //返回的参数
            $return_arr = (cUrl($url,$header=null, $app_data));
            S('city',$return_arr,86400);
        }
        foreach(S('city')['ReturnData'] as $key =>$val)
        {
            if($val['ProvID'] == $cid)
            {
               foreach($val['Citys'] as $k =>$v)
               {
                    if($v['CityID'] == $id)
                    {
                        $this->ajaxReturn($v['Dists']);
                        break;
                    }
               }
            }
        }
    }
    //添加仓库
    public function add_cangku()
    {
        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
        $post = I('post.');
        $post['FuncName'] = 'MYWH_ADD';
        $post['LoginID'] = $this->LoginID;
        //房产证图片
        $post['POC'] = S('仓库房产证-'.$this->LoginID)[0];
        foreach(S('仓库照片-'.$this->LoginID) as $key =>$val)
        {
            $Photos_path[] = array('Path'=>$val);
        }
        $post['Photos'] = $Photos_path;
        if(S('仓库平面图-'.$this->LoginID))
        {
            $post['Plan']  = S('仓库平面图-'.$this->LoginID)[0];
        }
        //dump($post);die;
        //116.407718,39.91582
        $post['Lng'] = I('post.jingdu');
        $post['Lat'] = I('post.weidu');
        $app_date = baseMD5($post);
        //dump($app_date);die;
        //返回的参数
        $url = C('api_url')['添加仓库'];
        $return_arr = cUrl($url,$header=null, $app_date);
        if($return_arr['ReturnCode'] == 0)
        {
            echo "<script>alert('添加成功');;location.href='/index.php?g=YunCangChu&m=Storehouse&a=mycangku'</script>";
        }
        else
        {
            echo "<script>alert('".$return_arr['ReturnText']."');;location.href='/index.php?g=YunCangChu&m=Storehouse&a=mycangku'</script>";
        }
    }
    //ajax图片上传 仓库添加
    public function ajaxImg()
    {
        if(I('post.upload') == 1)
        {
            $key='仓库照片-';
            $type = 'S';
        }
        elseif(I('post.upload') == 2)
        {
            $key='仓库房产证-';
            $type = 'P';
        }else
        {
            $key='仓库平面图-';
            $type = 'W';
        }
        $return = json_decode($this->upload(),true);
        if($return['code'] != 200)
        {
            $this->ajaxReturn(array('code'=>404,'msg'=>$return['msg']));
        }
        else
        {
            //图片url
            $file =  dirname(dirname(dirname(dirname(__FILE__)))).'/Uploads/'.$return['src'];
            $url = 'http://www.ees.com.cn/WMS/AppInterface/UploadPic.ashx';
            //$url = 'http://yuncangchu.com/index.php?g=YunCangChu&m=my&a=text';
            $post_data = array(
                "LoginID" => $this->LoginID,
                //要上传的本地文件地址
                "Photo" => "@".$file,
                'Type'=>$type
            );
            $ch = curl_init();
            curl_setopt($ch , CURLOPT_URL , $url);
            curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch , CURLOPT_POST, 1);
            curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            $output = json_decode($output,true);
            if($output['ReturnCode'] == 0)
            {
                $arr = S($key.$this->LoginID);
                $arr[]=$output['ReturnData'];
                S($key.$this->LoginID,$arr,7200);
                $this->ajaxReturn(array('code'=>200,'url'=>$output['ReturnData']));
            }
            else
            {
                $this->ajaxReturn(array('code'=>404,'msg'=>$output['ReturnText']));
            }
        }

    }
    //ajax图片上传 申请加盟
    public function ajaxImg1()
    {
        $file = $_FILES['ajaxImg1'];
        $upload = I('post.upload');
        if($upload == 1)
        {
            $key = '申请加盟身份证照片-'.$this->LoginID;
            $type = 'F';
        }
        elseif($upload == 2)
        {
            $key = '申请加盟身份证背面照-'.$this->LoginID;
            $type = 'B';
        }
        else
        {
            $key = '申请加盟手持身份证照-'.$this->LoginID;
            $type = 'I';
        }
        $return = json_decode($this->upload($file),true);
        if($return['code'] != 200)
        {
            $this->ajaxReturn(array('code'=>404,'msg'=>$return['msg']));
        }
        else
        {
            //图片url
            $file =  dirname(dirname(dirname(dirname(__FILE__)))).'/Uploads/'.$return['src'];
            $url = 'http://www.ees.com.cn/WMS/AppInterface/UploadPic.ashx';
            //$url = 'http://yuncangchu.com/index.php?g=YunCangChu&m=my&a=text';
            $post_data = array(
                "LoginID" => $this->LoginID,
                //要上传的本地文件地址
                "Photo" => "@".$file,
                'Type'=>$type
            );
            $ch = curl_init();
            curl_setopt($ch , CURLOPT_URL , $url);
            curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch , CURLOPT_POST, 1);
            curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            $output = json_decode($output,true);
            if($output['ReturnCode'] == 0)
            {
                $arr = S($key);
                $arr[]=$output['ReturnData'];
                S($key,$arr,7200);
                $this->ajaxReturn(array('code'=>200,'url'=>$output['ReturnData']));
            }
            else
            {
                $this->ajaxReturn(array('code'=>404,'msg'=>$output['ReturnText']));
            }
        }
    }
    //申请加盟add
    public function shenqQingAdd()
    {
        $url = C('api_url')['申请加盟'];
        $post = I('post.');
        $post['FuncName'] = 'JOIN_IN';
        $post['LoginID'] = $this->LoginID;
        $post['ID_PIC1'] =S('申请加盟身份证照片-'.$this->LoginID)[0];
        $post['ID_PIC2'] =S('申请加盟身份证背面照-'.$this->LoginID)[0];
        $post['ID_PIC3'] =S('申请加盟手持身份证照-'.$this->LoginID)[0];
        $app_date = baseMD5($post);
        $return_arr = cUrl($url,$header=null, $app_date);
        if($return_arr['ReturnCode'] == 0)
        {
            echo "<script>alert('申请成功');;location.href='/index.php?g=YunCangChu&m=Storehouse&a=mycangku'</script>";
        }
        else
        {
            echo "<script>alert('".$return_arr['ReturnText']."');;location.href='/index.php?g=YunCangChu&m=Storehouse&a=mycangku'</script>";
        }
    }
    public function kefu()
    {
        //echo file_get_contents('https://kefu.easemob.com/webim/im.html?configId=02e3671f-3441-425a-9ab7-5f2e166f5ea1');die;
        $this->display();
    }
    public function kefu1()
    {
        //echo file_get_contents('https://kefu.easemob.com/webim/im.html?configId=02e3671f-3441-425a-9ab7-5f2e166f5ea1');die;
        $this->display();
    }
    //测试
    public function test()
    {
        //dump(S('申请加盟身份证背面照-'.$this->LoginID));
    }
}